package com.saile.controller.admin.advert;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.saile.controller.base.BaseController;
import com.saile.def.Constant;
import com.saile.domain.Advert;
import com.saile.myRunTime.MyRunTimeExcption;
import com.saile.result.ResultObj;
import com.saile.service.AdvertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RequestMapping("/admin/advert")
@Controller
public class AdminAdvertContorller extends BaseController {
    @Autowired
    private AdvertService advertService;



    @RequestMapping(value = "/getAdvertList", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public String getAdvertList() {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            resultObj.setData(advertService.getAdvertList(this.showRequestParameters()));
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
        }
        return resultObj.toJson();

    }


    @RequestMapping(value = "/modifyAdvert")
    @ResponseBody
    public String modifyAdvert(Advert advert, MultipartFile location) {
        logger.info("=================================modifyAdvert==========================================>>>>>>");

        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_FAIL);
        String picUrl = "";
        try {

            try {
                String filePath = upLoadFile(location);
                picUrl = filePath;
                logger.info("resourceUrl=============>>>" + picUrl);
                advert.setPicUrl(picUrl);
            } catch (Exception e) {
                logger.info(e.getMessage());
                e.printStackTrace();
            }
            advertService.modifyAdvert(advert);

            resultObj.setCode(ResultObj.CODE_SUCCESS);
            resultObj.setMsg("成功！");
        } catch (MyRunTimeExcption myRunTimeExcption) {
            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

    @RequestMapping(value = "/advert")
    public ModelAndView advert(Long tid) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/admin/advert/advert");
        if (tid!=null) {
            try {
                Map<String,String> map = new HashMap<>();
                map.put("tid",String.valueOf(tid));
                Advert advert = null;
                advert = advertService.getAdvert(map);
                mov.addObject("advert", advert);
            } catch (MyRunTimeExcption e) {
                e.printStackTrace();
            }
        }


        return mov;

    }

    @RequestMapping(value = "/advertList")
    public ModelAndView advertList(Integer page, Integer pageSize) {
        logger.info("=============================showAdvertList=============================================>>>>>>");
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/admin/advert/advertList");

        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        PageHelper.startPage(page, pageSize);
        Map<String, String> map = this.showRequestParameters();
        List<Advert> adverList = advertService.getAdvertList(map);
        PageInfo<Advert> pageinfo = new PageInfo<Advert>(adverList);
        map.put("state","1");
        mov.addObject("advertList", pageinfo);
        mov.addObject("bean", map);
        return mov;
    }



    /**
     * 删除广告
     *
     * @param advertId
     * @return
     */
    @RequestMapping(value = "/deleteAdvert")
    @ResponseBody
    public String deleteAdvert(Long advertId) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            advertService.deleteAdvert(advertId);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }




}